package demo1;


import java.util.Scanner;

public class Test extends Exception{

    public static void main(String[] args) {
        try {
            int[] array = {1,2,3};
            System.out.println(array[3]); // 此处会抛出数组越界异常
        }catch (Exception e){ // Exception可以捕获到所有异常
            e.printStackTrace();
        }
        System.out.println("其他业务！");
    }

    public static void func3(int a) throws CloneNotSupportedException{
        if (a == 10) {
            throw new CloneNotSupportedException("a==10");
        }

    }

    public static void func2(int a) {
        try {
            if (a == 10) {
                throw new CloneNotSupportedException("a==10");
                //受查异常，需要处理
            }
        }catch (CloneNotSupportedException e) {
            e.printStackTrace();
        }
    }

    public static void main3(String[] args) {
        func2(10);

        try {
            func3(10);//受查异常，需要处理
        }catch (CloneNotSupportedException e){
            e.printStackTrace();
        }
        System.out.println("其他业务");
    }

    public static void func1(int a) {
        if (a == 10) {
            throw new NullPointerException("a==10");
        }
    }

    public static void main2(String[] args) {
        //int[] arr = {1, 2, 3, 4};
        try {
            //System.out.println(arr[9]);
            func1(10);
            System.out.println("hhhhh");
        }catch (ArrayIndexOutOfBoundsException | NullPointerException e){
            e.printStackTrace();
            System.out.println("捕获到了一个数组越界异常或者空指针异常！");
        }
        System.out.println("其他业务");
    }

    public static void main1(String[] args) {
        //int[] arr = {1, 2, 3, 4};
        try {
            //System.out.println(arr[9]);
            func1(10);
            System.out.println("hhhhh");
        }catch (ArrayIndexOutOfBoundsException e){
            e.printStackTrace();
            System.out.println("捕获到了一个数组越界异常！");
        }catch (NullPointerException e) {
            e.printStackTrace();
            System.out.println("捕获到了一个空指针异常！");
        }
        System.out.println("其他业务");
    }

    public static void func(int a) throws ArrayIndexOutOfBoundsException, NullPointerException{
        if (a == 10) {
            throw new ArrayIndexOutOfBoundsException("捕获到了一个数组越界异常！");
        }

        if(a == 20) {
            throw new NullPointerException("捕获到了一个空指针异常！");
        }
    }

    public static void main0(String[] args) throws ArrayIndexOutOfBoundsException, NullPointerException{
        int a = 10;
        func(a);
    }
}
